/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.csi.jf.crucible.common.config.JfConstants;
import com.csi.jf.crucible.common.model.JfTUser;
import com.csi.jf.crucible.ptservice.model.ProjectTemp;

/**
 * @Title: LoginUtils.java
 * @Description: <br>
 *               <br>
 * @Company: Chinasofti
 * @Created on 2014-7-14 下午7:43:55
 * @author zhangmh
 * @version $Revision: 1.0 $
 * @since 1.0
 */
public class LoginUtils {
    
    protected static final Logger LOG = LoggerFactory
            .getLogger(LoginUtils.class);
    /**
     * <p>获取登录用户</p>
     * 
     */
    public  static JfTUser getLoginUser() {
        JfTUser loginUser = new JfTUser();
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        if(request == null) {
            return null;    
        }
        //消除登录用户信息
        HttpSession session = request.getSession(false);
        if(null!=session){
            if(null!=session.getAttribute(JfConstants.LOGIN_USER)){
                loginUser = (JfTUser)session.getAttribute(JfConstants.LOGIN_USER);
            }
        }
        return loginUser;
    }
    
    /**
     * 更改当前项目ID
     * @param projectId
     */
    public static  void changeCurrentProjectID(Long projectId,String projectName) {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        if(request == null) {
            return;    
        }
        HttpSession session = request.getSession(false);
        if(session!=null){
            session.setAttribute(JfConstants.PROJECT_ID, projectId);
            session.setAttribute(JfConstants.PROJECT_NAME, projectName);
        }
    }
    
    /**
     * 返回当前项目ID和NAME
     * @return
     */
    public static ProjectTemp getCurrentProject() {
        ProjectTemp project = new ProjectTemp();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        if (request == null) {
            return null;
        }
        HttpSession session = request.getSession(false);
        if (session != null) {
            if (null != session.getAttribute(JfConstants.PROJECT_ID)
                    && null != session.getAttribute(JfConstants.PROJECT_NAME)) {
                project.setProjectId((Long) session
                        .getAttribute(JfConstants.PROJECT_ID));
                project.setProjectName((String) session
                        .getAttribute(JfConstants.PROJECT_NAME));
            } else {
                return null;
            }
        }
        return project;
    }
    

}
